import * as types from '../constants/actionTypes';
import urls from '../../app/constants/urls';
import { GET, } from '../../commons/utils/request';

const setLoginState = data => {
  return {
    type: types.SET_SIGIN_STATE,
    ...data,
  };
};

const setAuthState = data => {
  return {
    type: types.SET_AUTH_STATE,
    ...data,
  };
};

const setOpenAccountState = data => {
  return {
    type: types.SET_OPEN_ACCOUNT_STATE,
    ...data,
  };
};

const setCityState = data => {
  return {
    type: types.SET_CITY_STATE,
    ...data,
  };
};

const setCountryState = data => {
  return {
    type: types.SET_COUNTRY_STATE,
    ...data,
  };
};

const setChooseTypeState = data => {
  return {
    type: types.SET_CHOOSE_TYPE,
    ...data,
  };
};

const setDealOutState = data => {
  return {
    type: types.SET_DEAL_OUT_TYPE,
    ...data,
  };
};

const setSubmitState = data => {
  return {
    type: types.GET_SUBMIT_STATE,
    ...data,
  };
};

const login = params => {
  return dispatch => {
    return dispatch(
      setLoginState({
        logged: params.isLogin,
      }),
    );;
  };
};

const auth = params => {
  return dispatch => {
    return dispatch(
      setAuthState({
        authed: params.authed,
      }),
    );;
  };
};

const openAccount = params => {
  return dispatch => {
    return dispatch(
      setOpenAccountState({
        hasOpenAccount: params.hasOpenAccount,
      }),
    );
  };
};

const getCityList = params => {
  return dispatch => {
    return GET(urls.cityInfo, {}, null).then(async resp => {
      console.log('TAGGG', `getCityList====City====resp==${JSON.stringify(resp)}`);
      if (resp && resp.code == 0 && resp.data) {
        return dispatch(
          setCityState({
            cityList: resp.data,
          }),
        );
      } 
    }, error => {
      console.log('TAGGG', `getCityList====error==${JSON.stringify(error)}`);
    });
    
  };
};

const getCountryList = params => {
  return dispatch => {
    return GET(urls.countryInfo, {}, null).then(async resp => {
      console.log('TAGGG', `getCountryList==City==resp==${JSON.stringify(resp)}`);
      if (resp && resp.code == 0 && resp.data) {
        return dispatch(
          setCountryState({
            counyrtList: resp.data,
          }),
        );
      } 
    }, error => {
      console.log('TAGGG', `getCountryList====error==${JSON.stringify(error)}`);
    });
    
  };
};

const chooseType = params => {
  return dispatch => {
    return dispatch(
      setChooseTypeState({
        type: params.type,
      }),
    );
  };
};

const dealOut = params => {
  return dispatch => {
    return dispatch(
      setDealOutState({
        dealTime: params.dealTime,
      }),
    );
  };
};

const submitOption = params => {
  return dispatch => {
    return dispatch(
      setSubmitState({
        imgUploadTime: params.imgUploadTime,
      }),
    );
  };
};

module.exports = {
  login,
  auth,
  openAccount,
  getCityList,
  getCountryList,
  chooseType,
  dealOut,
  submitOption,
};
